package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.KeyValueVo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.security.annotation.PreAuthorize;
import com.ruoyi.system.domain.SysUserConfig;
import com.ruoyi.system.service.ISysUserConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户设置Controller
 * 
 * @author WJH
 * @date 2022-08-02
 */
@RestController
@RequestMapping("/system/SysUserConfig")
public class SysUserConfigController extends com.ruoyi.common.core.web.controller.BaseController
{
    @Autowired
    private ISysUserConfigService sysUserConfigService;

    /**
     * 查询用户设置列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:SysUserConfig:list")
    //@ApiOperation(value = "获取用户设置列表")
    @GetMapping("/getSysUserConfigList")
    public com.ruoyi.common.core.web.page.TableDataInfo getSysUserConfigList(SysUserConfig sysUserConfig)
    {
        startPage();
        List<SysUserConfig> list = sysUserConfigService.selectSysUserConfigList(sysUserConfig);
        return getDataTable(list);
    }


    /**
     * 获取用户设置详细信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:SysUserConfig:query")
    //@ApiOperation(value = "获取用户设置详情")
    @GetMapping(value = "/getSysUserConfigById")
    public com.ruoyi.common.core.web.domain.AjaxResult getSysUserConfigById(@RequestParam("userId") Long userId)
    {
        return com.ruoyi.common.core.web.domain.AjaxResult.success(sysUserConfigService.selectSysUserConfigByUserId(userId));
    }

    /**
     * 新增用户设置
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:SysUserConfig:add")
    @Log(title = "用户设置", businessType = BusinessType.INSERT)
    //@ApiOperation(value = "新增用户设置")
    @PostMapping(value = "/addSysUserConfig")
    public com.ruoyi.common.core.web.domain.AjaxResult addSysUserConfig(@RequestBody SysUserConfig sysUserConfig)
    {
        return toAjax(sysUserConfigService.insertSysUserConfig(sysUserConfig));
    }

    /**
     * 修改用户设置
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:SysUserConfig:edit")
    @Log(title = "用户设置", businessType = BusinessType.UPDATE)
    //@ApiOperation(value = "修改用户设置")
    @PostMapping(value = "/editSysUserConfig")
    public com.ruoyi.common.core.web.domain.AjaxResult editSysUserConfig(@RequestBody SysUserConfig sysUserConfig)
    {
        return toAjax(sysUserConfigService.updateSysUserConfig(sysUserConfig));
    }

    /**
     * 删除用户设置
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:SysUserConfig:remove")
    @Log(title = "用户设置", businessType = BusinessType.DELETE)
	@GetMapping("/removeSysUserConfigs")
    public com.ruoyi.common.core.web.domain.AjaxResult removeSysUserConfigs(@RequestParam Long[] userIds)
    {
        return toAjax(sysUserConfigService.deleteSysUserConfigByUserIds(userIds));
    }
    @GetMapping("/getSysUserConfigNamesForSelect")
    //@ApiOperation(value = "获取用户设置名称列表用于下拉搜索")
    public com.ruoyi.common.core.web.domain.AjaxResult getSysUserConfigNamesForSelect()
    {
        List<SysUserConfig> list = sysUserConfigService.list();//自行加条件
        List<KeyValueVo> resultList = new ArrayList<>();
        for (SysUserConfig sysUserConfig: list ) {
            KeyValueVo vo = new KeyValueVo();
            //vo.setKey(it.getId());
            //vo.setValue(it.getName());
            resultList.add(vo);
        }
        return com.ruoyi.common.core.web.domain.AjaxResult.success(resultList);
    }
    /**
     * 导出用户设置列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:SysUserConfig:export")
    @Log(title = "用户设置", businessType = BusinessType.EXPORT)
    @GetMapping(value = "/exportSysUserConfig", produces = "application/octet-stream")
    public String exportSysUserConfig(HttpServletResponse response, SysUserConfig sysUserConfig) throws IOException
    {
        List<SysUserConfig> list = sysUserConfigService.selectSysUserConfigList(sysUserConfig);
        com.ruoyi.common.core.utils.poi.ExcelUtil<SysUserConfig> util = new com.ruoyi.common.core.utils.poi.ExcelUtil<SysUserConfig>(SysUserConfig.class);
        util.exportExcel(response, list, "用户设置数据");
        return "";
    }

}
